package com.caiwangdai.app.ui.home.me

import android.os.Handler
import com.caiwangdai.app.R
import com.caiwangdai.app.backend.RetrofitHome
import com.caiwangdai.app.backend.observer.OnDataV2Listener
import com.caiwangdai.app.backend.rx.RxSchedulers
import com.caiwangdai.app.bean.UserInfoB
import com.caiwangdai.app.bean.base.BaseB
import com.caiwangdai.app.bean.base.NormalResponseB
import com.caiwangdai.app.extension.logOut
import com.caiwangdai.app.extension.setOnClickListenerPro
import com.caiwangdai.app.extension.toast
import com.caiwangdai.app.origin.BaseFragment
import com.caiwangdai.app.ui.MainActivity
import com.caiwangdai.app.ui.borrow.BorrowListActivity
import com.caiwangdai.app.ui.me.BankInfoActivity
import com.caiwangdai.app.ui.me.IdentityInfoActivity
import com.caiwangdai.app.utils.AuthUtils
import kotlinx.android.synthetic.main.fragment_me.view.*


class MeFragment : BaseFragment() {

    override fun getLayoutId() = R.layout.fragment_me

    companion object {
        fun newInstance(): MeFragment {
            val intent = MeFragment()
            return intent
        }
    }


    override fun initView() {
        //银行卡信息
        WMContent.rela_bank.setOnClickListenerPro {
            startActivity(BankInfoActivity.newInstance(mActivity))
        }
        //借款信息
        WMContent.rela_borrow_info.setOnClickListenerPro {
            startActivity(BorrowListActivity.newInstance(mActivity))
        }
        //身份信息
        WMContent.rela_id_info.setOnClickListenerPro {
            startActivity(IdentityInfoActivity.newInstance(mActivity))
        }
        //退出登录
        WMContent.rela_log_out.setOnClickListenerPro {
            logout()
        }
    }


    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden) getUserInfo()//获取信息
    }

    //获取用户信息
    private fun getUserInfo() {
        RetrofitHome.getUserApi()
                .getUserInfo()
                .compose(RxSchedulers.toMain())
                .subscribe(object : OnDataV2Listener<UserInfoB>(mActivity, WMContent.swipe_refresh) {

                    override fun isShowProgressBar() = false
                    override fun onSwipeRefresh() {
                        getUserInfo()
                    }

                    override fun onSuccess(value: NormalResponseB<UserInfoB>) {
                        val data = value.data?.info
                        //mobile
                        val mobile = AuthUtils.hideMobileNo(data?.mobilePhone)
                        WMContent.tv_mobile.text = mobile
                        //balance
                        WMContent.tv_balance.text = data?.money
                        //loan
                        WMContent.tv_loan_money.text = data?.loan_money
                    }

                })
    }

    //退出登录
    private fun logout() {
        RetrofitHome.getUserApi()
                .logout()
                .compose(RxSchedulers.toMain())
                .subscribe(object : OnDataV2Listener<BaseB>(mActivity) {
                    override fun getProgressBarContent() = "退出中..."

                    override fun onComplete() {
                        Handler().postDelayed({
                            super.onComplete()
                            toast("退出成功")
                            mActivity.logOut()
                            (mActivity as MainActivity).setCurrentFragment(0)
                        }, 500)

                    }
                })
    }




}